package com.game.db.mybatis;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.game.db.mybatis.mapper.UserMapper;
import com.game.db.mybatis.pojo.User;

public class MyBatisDbSource {
	
	private static SqlSessionFactory sessionFactory = null;
	
	static{
		Reader reader;
		try {
			reader = Resources.getResourceAsReader("mybatis.cfg.xml");
			sessionFactory = new SqlSessionFactoryBuilder().build(reader);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static SqlSession getSession(){
		return sessionFactory.openSession();
	}
	
	public static void main(String[] args) {
		SqlSession session = getSession();
		
		SqlSession session2 = getSession();
		
		SqlSession session3 = getSession();
		
		UserMapper mapper = session.getMapper(UserMapper.class);
		User user = mapper.selectUserById(1);
		
		List<User> users = mapper.selectSameAgeUsers(11);
		users = mapper.selectAllUsers();
//		users = mapper.selectSameUsers("222", 11);
		
//		for(User u : users){
//			System.out.println(u.toString());
//			int result = mapper.updateUser(u,"newName");
//			System.err.println(result);
//		}
		int nextUserId = users.size()+1;
		
//		mapper.insertUser(new User(nextUserId,"add",15));
		int result = mapper.deleteUser(4L);
		System.err.println(result);
	}
}

